package com.xiegk.controller;

import com.xiegk.ticket.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 模拟买票加锁 - demo
 *
 * @author xgk
 * @date 2022/2/20
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("demos")
public class DemoController {

    private final TicketSync01 localLock01;
    private final TicketReeLock02 localLock02;
    private final TicketRedisLockv01 redisLock01;
    private final TicketRedisLockv02 redisLock02;
    private final TicketRedissonLock redisLock03;

    /**
     * 模拟买票 - 使用Apache JMeter 模拟并发请求
     */
    @GetMapping
    public String demo() {
        // 测试一: synchronized
        localLock01.sale();

        // 测试二: ReentrantLock
        localLock02.sale();

        // 测试三: redisLock v1
        redisLock01.sale();

        // 测试四: redisLock v2
        redisLock02.sale();

        // 测试五: redissonLock v3
        redisLock03.sale();
        return "欢迎购票";
    }

}
